﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Windows.Browser;
using System.Runtime.Serialization;
using System.IO;
using System.Linq;
using System.Text;
namespace PPFinance.Common
{
    public class Cookies
    {
        const string COOKIE_KEY = "SLCookie";
        List<Cookie> cookies = new List<Cookie>();
        public Cookies()
        {

            try
            {
                string cookieString = Read(COOKIE_KEY);
                cookies = JsonHelper.Deserialize<List<Cookie>>(cookieString);
            }
            catch { }
        }

        public string this[string key]
        {
            get
            {
                var cs = cookies.Where(p => p.Key.ToLower().Trim() == key.ToLower().Trim());
                if (cs.Any())
                {
                    return cs.Take(1).Single().Value;
                }
                return string.Empty;
            }
            set
            {
                var cs = cookies.Where(p => p.Key.ToLower().Trim() == key.ToLower().Trim());
                if (cs.Any())
                {
                    cs.Take(1).Single().Value = value;
                }
                else
                {
                    cookies.Add(new Cookie() { Key = key, Value = value });
                }
                SaveCookies();
            }
        }
        public void Add(Cookie item)
        {
            this[item.Key] = item.Value;
        }
        private void SaveCookies()
        {
            Write(COOKIE_KEY, JsonHelper.Serialize(cookies), 365);
        }

        private bool Exists(string key, string value)
        {
            return HtmlPage.Document.Cookies.Contains(key + "=" + value);
        }

        private string Read(string key)
        {
            string[] cookies = HtmlPage.Document.Cookies.Split(';');
            foreach (string cookie in cookies)
            {
                string[] keyValuePair = cookie.Split('=');
                if (keyValuePair.Length == 2 && key == keyValuePair[0].Trim())
                    return Uri.UnescapeDataString(keyValuePair[1].Trim());
            }

            return null;
        }

        private void Write(string key, string value, int expireDays)
        {

            value = Uri.EscapeDataString(value);

            // expireDays = 0, indicates a session cookie that will not be written to disk 
            // expireDays = -1, indicates that the cookie will not expire and will be permanent
            // expireDays = n, indicates that the cookie will expire in “n” days
            string expires = "";
            if (expireDays != 0)
            {
                DateTime expireDate = (expireDays > 0 ?
                DateTime.Now + TimeSpan.FromDays(expireDays) :
                DateTime.MaxValue);
                expires = ";expires=" + expireDate.ToString("R");
            }

            string cookie = key + "=" + value + expires;
            HtmlPage.Document.SetProperty("cookie", cookie);
        }

        private void Delete(string key)
        {
            DateTime expireDate = DateTime.Now - TimeSpan.FromDays(1); // yesterday
            string expires = ";expires=" + expireDate.ToString("R");
            string cookie = key + "=" + expires;
            HtmlPage.Document.SetProperty("cookie", cookie);
        }

    }

    [DataContract()]
    public class Cookie
    {
        public Cookie()
        { }
        public Cookie(string key, string value)
        {
            this.Key = key;
            this.Value = value;
        }
        [DataMember()]
        public string Key { get; set; }
           [DataMember()]
        public string Value { get; set; }
    }
}
